package com.hznu.ez4life.modules.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.hznu.ez4life.modules.entity.Item;
import com.hznu.ez4life.modules.service.ItemService;
import com.hznu.ez4life.myBeans.R;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author DragonDoor
 * @since 2019-07-25
 */
@RestController
@EnableAutoConfiguration
@RequestMapping("/item")
public class ItemController {

    @Autowired
    ItemService itemService;

    @GetMapping("/findList/{userId}")
    public R findAll(@PathVariable Integer userId)
    {
        try
        {
            return R.ok().put("itemList",itemService.findList(userId));
        }catch (Exception e)
        {
            return R.error();
        }
    }

    @GetMapping("/findOne/{id}")
    public R findOne(@PathVariable Integer id)
    {
        try
        {
            return R.ok().put("item",itemService.findById(id));
        }catch (Exception e)
        {
            e.printStackTrace();
            return R.error();
        }
    }

    @PostMapping("/addOne/{userid}")
    public R addOne(@PathVariable Integer userid,@RequestBody Item item)
    {
        try
        {
            item.setUserId(userid);
            itemService.insert(item);
            return R.ok();
        }catch (Exception e)
        {
            e.printStackTrace();
            return R.error();
        }
    }

    @GetMapping("/deleteOne/{id}")
    public R deleteOne(@PathVariable Integer id)
    {
        try
        {
            itemService.deleteById(id);
            return R.ok();
        }catch (Exception e)
        {
            e.printStackTrace();
            return R.error();
        }
    }

}

